import {messages} from "share/common";
import httpFetch from "share/httpFetch";
import config from "config";
import errorMessage from "share/errorMessage";
import { commonBarReportFactory, commonPieReportFactory, contractSignDateReportFactory, commonBarSeriesReportFactory } from "../echarts-data-transfer";

export default {
  //合同签署日期
  getContractSignDateReport(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/contract/due/search', params)
        .then((response) => {
          let res = {};
          let data = response.data || [];
          res.dataLength = data.length;
          res.options = contractSignDateReportFactory(data);
          resolve(res);
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  //合同状态分析
  getContractStatusReport(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/contract/status/search', params)
        .then((response) => {
          let res = {};
          let data = response.data || [];
          res.dataLength = data.length;
          res.options = commonPieReportFactory(data, true, messages('finance-7.key212')/*合同统计(个)*/);
          resolve(res);
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  // 合同公司分析
  getContractCompanyReport(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/contract/company/search', params)
        .then((response) => {
          let res = {};
          // 柱状图绘制的时候，是从原点开始的。在前面的数据会离坐标轴更近，导致数据最后绘制出来，纵轴的顺序和数据原来的顺序是反着的，所以先反序一下
          let data = response.data ? response.data.reverse() : [];
          res.dataLength = data.length;
          let extraData = {
            hidePercent: true
          }
          res.options = commonBarSeriesReportFactory(
            data,
            [messages('finance-7.key213')/*应付(万元)*/, messages('finance-7.key214')/*已付(万元)*/],
            extraData
          );
          resolve(res);
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  // 合同类型分析
  getContractTypeReport(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/contract/type/search', params)
        .then((response) => {
          let res = {};
          let data = response.data ? response.data.reverse() : [];
          res.dataLength = data.length;
          let extraData = {
            hidePercent: true
          }
          res.options = commonBarSeriesReportFactory(
            data,
            [messages('finance-7.key213')/*应付(万元)*/, messages('finance-7.key214')/*已付(万元)*/],
            extraData
          );
          resolve(res);
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  //合同数据明细统计表
  getContractDetailReport(params, pagination){
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/contract/detail' + '?page=' + pagination.page + "&size=" + pagination.size, params)
        .then((response) => {
          response.data = response.data.filter(item => {
            if (item + "" !== "null" && item + "" !== "undefined") {
              return item;
            }
          })
          resolve(response)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  }
}
